package com.wind.sqlite.service.impl;

import com.wind.sqlite.common.base.BaseModelPageDTO;
import com.wind.sqlite.common.utils.PageUtils;
import com.wind.sqlite.common.utils.Query;
import com.wind.sqlite.dao.WhiteListTDao;
import com.wind.sqlite.entity.WhiteListTEntity;
import com.wind.sqlite.common.base.BaseModelPageDTO;
import com.wind.sqlite.common.utils.PageUtils;
import com.wind.sqlite.common.utils.Query;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import com.wind.sqlite.dao.WhiteListTDao;
import com.wind.sqlite.entity.WhiteListTEntity;
import com.wind.sqlite.service.WhiteListTService;


@Service("whiteListTService")
public class WhiteListTServiceImpl extends ServiceImpl<WhiteListTDao, WhiteListTEntity> implements WhiteListTService {

  @Autowired
  private  WhiteListTDao whiteListTDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<WhiteListTEntity> page = this.page(
                new Query<WhiteListTEntity>().getPage(params),
                new QueryWrapper<WhiteListTEntity>()
        );
        return new PageUtils(page);
    }

  @Override
  public PageUtils queryPage(BaseModelPageDTO<WhiteListTEntity> data) {
    WhiteListTEntity entity = data.getData();
    QueryWrapper<WhiteListTEntity> queryWrapper = null;
    //分页查询所有
    if(entity==null){
      queryWrapper = new QueryWrapper<WhiteListTEntity>();
    }else{
      queryWrapper = new QueryWrapper<WhiteListTEntity>();
      queryWrapper = getWhiteListTEntityQueryWrapper(entity, queryWrapper);
    }
    IPage<WhiteListTEntity> page = this.page(
        new Query<WhiteListTEntity>().getPage(data),
        queryWrapper
    );
    return new PageUtils(page);
  }

  @Override
  public PageUtils queryPage(WhiteListTEntity entity) {
    BaseModelPageDTO<WhiteListTEntity> data=new BaseModelPageDTO<WhiteListTEntity>();
    data.setData(entity);
    return queryPage(data);
  }

    @Override
    public int remove(WhiteListTEntity entity) {
        QueryWrapper<WhiteListTEntity> queryWrapper = null;
        //分页查询所有
        if(entity==null){
            queryWrapper = new QueryWrapper<WhiteListTEntity>();
        }else{
            queryWrapper = new QueryWrapper<WhiteListTEntity>();
            queryWrapper = getWhiteListTEntityQueryWrapper(entity, queryWrapper);
        }
        return whiteListTDao.delete(queryWrapper);
    }

    @Override
    public List<WhiteListTEntity> query(WhiteListTEntity entity) {
        QueryWrapper<WhiteListTEntity> queryWrapper = null;
        //分页查询所有
        if(entity==null){
            queryWrapper = new QueryWrapper<WhiteListTEntity>();
        }else{
            queryWrapper = new QueryWrapper<WhiteListTEntity>();
            queryWrapper = getWhiteListTEntityQueryWrapper(entity, queryWrapper);
        }
        return whiteListTDao.selectList(queryWrapper);
    }

    private QueryWrapper<WhiteListTEntity> getWhiteListTEntityQueryWrapper(WhiteListTEntity entity, QueryWrapper<WhiteListTEntity> queryWrapper) {
      if(entity.getProcessId()!=null){
        queryWrapper=queryWrapper.eq("process_id", entity.getProcessId());
       }
    if(StringUtils.isNotEmpty(entity.getIp())){
       queryWrapper=queryWrapper.eq("ip", entity.getIp());
       }
        if(entity.getCreateTimeBeginTime()!=null){
           queryWrapper=queryWrapper.ge("create_time", entity.getCreateTimeBeginTime());
           }
        if(entity.getCreateTimeEndTime()!=null){
           queryWrapper=queryWrapper.le("create_time", entity.getCreateTimeEndTime());
           }
        if(entity.getUpdateTimeBeginTime()!=null){
           queryWrapper=queryWrapper.ge("update_time", entity.getUpdateTimeBeginTime());
           }
        if(entity.getUpdateTimeEndTime()!=null){
           queryWrapper=queryWrapper.le("update_time", entity.getUpdateTimeEndTime());
           }
    return queryWrapper;
  }


}